package org.minos.edit.view
{
	import flash.events.Event;
	import org.minos.edit.EditFacate;
	import org.minos.edit.view.components.Menu;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class MenuMediator extends Mediator
	{
		public static const NAME : String = "MenuMediator";
		
		public function MenuMediator( v : Menu )
		{
			super( NAME , v );
		}
		
		public function get menu() : Menu
		{
			return viewComponent as Menu;
		}
		
		override public function onRegister() : void
		{
			super.onRegister();
			menu.getRemoveMenu().addActionListener( __onRemoveItem );
			menu.getAddMenu().addActionListener( __onAddItem );
			menu.getEditMenu().addActionListener( __onEditItem );
			menu.getOpenMenu().addActionListener( __onOpenItem );
			menu.getSaveMenu().addActionListener( __onSaveItem );
			menu.getNewMenu().addActionListener( __onNewItem );
		}
		
		override public function onRemove() : void
		{
			super.onRemove();
			menu.getEditMenu().removeActionListener( __onRemoveItem );
			menu.getAddMenu().removeActionListener( __onAddItem );
			menu.getRemoveMenu().removeActionListener( __onRemoveItem );
		}
		
		override public function listNotificationInterests() : Array
		{
			return [ EditFacate.SCENE_SELECTION_CHANGED , EditFacate.FILE_CHANGED ];
		}
		
		override public function handleNotification( notification : INotification ) : void
		{
			super.handleNotification( notification );
			var selection : Boolean = ( notification.getBody() != null );
			switch ( notification.getName() )
			{
				case EditFacate.SCENE_SELECTION_CHANGED: 
					menu.getEditMenu().setEnabled( selection );
					menu.getRemoveMenu().setEnabled( selection );
					break;
				case EditFacate.FILE_CHANGED: 
					menu.getSaveMenu().setEnabled( selection );
					break;
			}
		}
		
		// ----- event methods ----- //
		
		private function __onNewItem( e : Event ) : void
		{
			sendNotification( EditFacate.NEW_FILE );
		}
		
		private function __onEditItem( e : Event ) : void
		{
			sendNotification( EditFacate.EDIT_SCENE_VO );
		}
		
		private function __onAddItem( e : Event ) : void
		{
			sendNotification( EditFacate.ADD_SCENE_VO );
		}
		
		private function __onRemoveItem( e : Event ) : void
		{
			sendNotification( EditFacate.REMOVE_SCENE_VO );
		}
		
		private function __onOpenItem( e : Event ) : void
		{
			sendNotification( EditFacate.OPEN_FILE );
		}
		
		private function __onSaveItem( e : Event ) : void
		{
			sendNotification( EditFacate.SAVE_FILE );
		}
	
	}

}